package com.wangyf.thread.c_023_question;

/**
 * 两个线程打印 a1b2c3...z26
 * @author wangyf
 * @date 2021/1/21 21:12
 */
public class T01_waitnotify {
    static Thread t1 = null,t2 = null;
    public static void main(String[] args) {

        final Object lock = new Object();

        t1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    for (int i = 1; i <= 26; i++) {
                        System.out.print(i);
                        lock.notify();
                        lock.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();

                }

            }
        });
        t2 = new Thread(() -> {
            boolean t1Open = false;
            // 小写 97  大写 65
            synchronized (lock) {
                try {
                    for (int i = 97; i <= 97+26-1; i++) {
                        System.out.print((char) i);
                        if (!t1Open) {
                            t1.start();
                            t1Open = true;
                        }
                        lock.notify();
                        lock.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();
    }
}
